from flask import Blueprint, render_template, jsonify, request, session

from models.entity.district import District
from models.entity.user import User
from models.entity.patrol_recoder import PatrolRecoder
from models.entity.patrol_manage import PatrolManage
from models.entity.device import Device
from models.entity.facility import Facility
from models.entity.facility_with_device import FacilityWithDevice
import json
from appserver import db_store
from flask import abort
import datetime
from utils.result import result


mod = Blueprint("core_patrol_recorder", __name__, url_prefix="/core/patrol_recorder", template_folder="templates",
                static_folder="static")

# 这个项目用于巡检记录管理


@mod.route('/add/', methods=['POST'])
def add_partrol_recoder():
    """增加记录"""
    # {id,device_id,patrol_user_id,patrol_manage_id}
    receive = request.data.decode("utf-8")
    try:
        dict = json.loads(receive)
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    if "id" in dict:
        dict.pop("id")
    device = Device.get_device_by_id(dict['device_id'])
    if not device:
        return result(success=0, error_code=-1, error_msg="对应的设备不存在，请检查")
    if not User.get_entity_by_id(dict['patrol_user_id']):
        return result(success=0, error_code=-1, error_msg="指定用户不存在")
    # 得到巡检项id
    patrol_manage_id =dict['patrol_manage_id']
    if len(PatrolRecoder.query.filter_by(device_id=device.id).filter_by(patrol_manage_id=patrol_manage_id).filter_by(patrol_time=datetime.datetime.now().strftime("%Y-%m-%d")).all())!=0:
        # 如果同一天同一设备被巡检了两次
        return result(success=0, error_code=-2, error_msg="同一设备同一天只能被巡检一次")

    partrol_recoder = PatrolRecoder()
    try:
        partrol_recoder.from_dict(dict)
    except RuntimeError as e:
        return result(success=0, error_code=-1, error_msg=str(e))
    partrol_recoder.patrol_content = PatrolManage.get_entity_by_id(dict["patrol_manage_id"]).content
    if partrol_recoder.save():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/del/', methods=['POST'])
def del_partrol_recoder():
    """删除记录"，不知道能不能用"""
    """
    {
    "id": 5
    }
    """
    receive = request.data.decode("utf-8")
    try:
        dict = json.loads(receive)
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    patrol_recoder = PatrolRecoder.get_entity_by_id(dict['id'])
    if not patrol_recoder:
        return result(success=0, error_code=-1, error_msg="当前记录不存在")
    if patrol_recoder.delete():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/get_by_device_id/', methods=['POST'])
def get_recorder():
    """传入的设备id"""
    receive = request.data.decode("utf-8")
    try:
        dict = json.loads(receive)
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    patrol_recoders = PatrolRecoder.get_patrol_recoders_by_device_id(dict["id"])
    t_list = []
    for item in patrol_recoders:
        t_list.append(item.to_dict())
    return result(success=1, data=t_list)

"""
@mod.route("/get_by_district/",methods=['GET', 'POST'])
def get_result_recoder():
    # 得出记录统计结果

    tlist=db_store.session.execute("SELECT patrol_time FROM patrol_recoder GROUP BY patrol_time;").fetchall()
    print('tlist', tlist)
    checked_days=[]
    for tt in tlist:
        checked_days.append(tt[0])
    all_time_list=[]
    for day in checked_days:
        one_day_dic={}    # {facility_id:[[device_id_1,device_id_2],facility_name,facility_location,should_check_num,uncheck_num,checked_num,check_user,unchecked_patrol_manage_num,if_complete,remark,date], ...}
        recoder_in_one_day=PatrolRecoder.query.filter_by(patrol_time=day).all()
        facility_in_one_day=set()    # 当天巡检过的设施id的集合
        device_in_one_day=set()    # 当天巡检过的设备id的集合
        for recoder in recoder_in_one_day:
            t=FacilityWithDevice.query.filter_by(device_id=recoder.device_id).first().facility_id
            facility_in_one_day.add(t)
            device_in_one_day.add(recoder.device_id)
        t_list = []
        for in_facility_id in facility_in_one_day:
            for t in device_in_one_day:
                for l in PatrolManage.query.filter_by(device_type_id=Device.get_device_by_id(t).device_type_id).all():
                    if len(PatrolRecoder.query.filter_by(device_id=t).filter_by(patrol_manage_id=l.id).filter_by(patrol_time=day).all())!=1:
                        print("[ ERROR ] 同一设备同一天被巡检多次")
                        return abort(500)
            one_day_dic[in_facility_id]=[]
            one_day_dic[in_facility_id].append([d for d in device_in_one_day if FacilityWithDevice.query.filter_by(device_id=d).first().facility_id==in_facility_id])    # should checked device id
            f=Facility.get_facility_by_id(in_facility_id)
            one_day_dic[in_facility_id].append(f.name)    # facility name
            one_day_dic[in_facility_id].append("( "+str(f.lat)+","+str(f.lng)+" )")    # facility location
            one_day_dic[in_facility_id].append(len(FacilityWithDevice.get_device_by_facility(in_facility_id)))    # should check num
            print(one_day_dic[in_facility_id])
            one_day_dic[in_facility_id].append(one_day_dic[in_facility_id][3]-len(one_day_dic[in_facility_id][0]))    # uncheck num
            one_day_dic[in_facility_id].append(len(one_day_dic[in_facility_id][0]))    # checked num
            user_list=[]
            for t in one_day_dic[in_facility_id][0]:
                user_list.append(User.query.filter_by(id=PatrolRecoder.query.filter_by(device_id=t).filter_by(patrol_time=day).first().patrol_user_id).first().username)
            one_day_dic[in_facility_id].append(",".join(list(set(user_list))))    # check user
            should_check_patrol_manage_num=0
            for t in FacilityWithDevice.get_device_by_facility(in_facility_id):
                should_check_patrol_manage_num+=len(PatrolManage.query.filter(PatrolRecoder.device_id==t.id).filter(PatrolRecoder.result!=0).all())
            one_day_dic[in_facility_id].append(should_check_patrol_manage_num)
            has_checked_patrol_manage_num=0
            for t in one_day_dic[in_facility_id][0]:
                has_checked_patrol_manage_num+=len(PatrolRecoder.query.filter_by(device_id=t).filter_by(patrol_time=day).all())
            one_day_dic[in_facility_id].append(should_check_patrol_manage_num-has_checked_patrol_manage_num)     # unchecked patrol manage num
            one_day_dic[in_facility_id].append(int(should_check_patrol_manage_num>has_checked_patrol_manage_num))    # if complete 0-is complete 1-not complete
            for t in one_day_dic[in_facility_id][0]:
                recoder_of_the_device_in_the_day_list=PatrolRecoder.query.filter_by(device_id=t).filter(PatrolRecoder.result!=0).filter_by(patrol_time=day).all()
                for m in recoder_of_the_device_in_the_day_list:
                    if m.result!=0:
                        if m.remark is None:
                            t_list.append("没有故障说明")
                        else:
                            t_list.append(m.remark)
            t_str=",".join(t_list)
            one_day_dic[in_facility_id].append(t_str)    # patrol remark
            one_day_dic[in_facility_id].append(day.strftime("%Y-%m-%d"))    # patrol date
        for t in one_day_dic:
            tdic={}
            tdic["facility_name"]=one_day_dic[t][1]
            tdic["facility_location"]=one_day_dic[t][2]
            tdic["facility_device_amount"]=one_day_dic[t][3]
            tdic["uncheck_device"]=one_day_dic[t][4]
            tdic["check_device"]=one_day_dic[t][5]
            tdic["facility_user"]=one_day_dic[t][6]
            tdic["mistake"]=one_day_dic[t][7]
            tdic["is_complete"]=one_day_dic[t][9]
            tdic["detail"]=one_day_dic[t][10]
            tdic["date"]=one_day_dic[t][11]
            all_time_list.append(tdic)
    # rdic={}
    # rdic["success"]=1
    # rdic["error_code"]=None
    # rdic["error_msg"]=None
    # rdic["data"]=all_time_list
    return result(success=1, data=all_time_list)
"""
# {facility_id:[[device_id_1,device_id_2],facility_name,facility_location,should_check_num,uncheck_num,checked_num,check_user,unchecked_patrol_manage_num,if_complete,remark,date], ...}


"""
@mod.route('/get_by_district/', methods=['POST'])
def get_result_recoder():
    # 传入的分区id   研究了好久这版本
    data = []
    tmp = {}
    if request.method == 'POST':
        dict = json.loads(request.data.decode("utf-8"))
        print('dict', dict)
        if 'id' not in dict:
            tmp['success'] = 0
            tmp['error_code'] = -9
            tmp['error_msg'] = "数据格式错误，请重新选择"
            tmp['data'] = data
            return jsonify(tmp)
        id = dict['id']  # 得到的是分区的id
        # 通过分区获取设施
        facilities = Facility.get_facilities_by_district_id(id)# 得到该分区下的设施列表
        print("facilities",  facilities)
        # 总的设备数量
        patrol_device_items = {}   # 得到device_id的集合
        more_massage = {}
        for facility in facilities:
            # 遍历所有的设施然后通过第三张表得到每个设施下的装备 device.id
            facility_with_devices = FacilityWithDevice.get_device_by_facility(facility.id)
            for item in facility_with_devices: # item 现在是device实例
                print('item', item)
                # device_id = item.id
                # device = Device.get_device_by_id(device_id) # 得到具体的设备
                patrol_device_items_list = []
                print("device.device_type_id:" + str(item.device_type_id))
                patrol_manages = PatrolManage.get_patrol_manage_by_device_type(item.device_type_id)   # 根据device.type得到具体的巡检项
                print('patrol_manages', patrol_manages)
                for item1 in patrol_manages:
                    print(11,item1) # <PatrolManage 测试巡检项1> 2,3
                    patrol_device_items_list.append(item1.id)  # id 为 2,3,4
                    # patrol_device_items_list.append(item)  # 我的
                    # 巡检设备字典:设施的id作为建,巡检的内容为列表
                    patrol_device_items[facility.id] = patrol_device_items_list
        tlist=db_store.session.execute("SELECT patrol_time FROM patrol_recoder GROUP BY patrol_time;").fetchall()
        print('tlist', tlist)
        # tlist [(datetime.date(2017, 1, 1),), (datetime.date(2018, 1, 1),), (datetime.date(2018, 5, 22),), (datetime.date(2018, 5, 23),)]
        patrol_dates=[]
        for tt in tlist:
            print('tt[0]', tt[0]) # tt[0] 2017-01-01
            patrol_dates.append(tt[0])
            # print(111111)
        for patrol_date in patrol_dates:
            # print("cccccccccccccccccccccccccccccccccccccccc")
            # Object <PatrolManage 测试巡检项1> is not legal as a SQL literal value
            for facility in facilities:
                print('33facility', facility)
                lng = facility.lng
                lat = facility.lat
                facility_name = facility.name
                facility_user = ''
                facility_location = "(" + str(lng) + "," + str(lat) + ")"  # 这里显示设施的地址
                username =[]
                mistake = 0
                print('-------')
                device_list_day = list(set(patrol_device_items[facility.id]))
                # device_list_day = list(patrol_device_items[facility.id])  # 我的
                print('******')
                partrol_table_actual = []
                print(444)
                # patrol_recoders = PatrolRecoder.query.filter(PatrolRecoder.patrol_time == patrol_date).filter(PatrolRecoder.device_id.in_(device_list_day)).all()  #将该天的记录全都拿出来
                patrol_recoders = PatrolRecoder.query.filter(PatrolRecoder.patrol_time == patrol_date).all()  #将该天的记录全都拿出来

                print(22222)
                for patrol_recoder in patrol_recoders:
                    partrol_table_actual.append(patrol_recoder.device_id)
                    facility_user = patrol_recoder.patrol_user_id
                    username.append(User.get_user_by_id(facility_user).username)
                    if patrol_recoder.remark is not None:
                        more_massage[Device.get_device_by_id(patrol_recoder.device_id).name]=patrol_recoder.remark   #将对应的设备问题找出来
                    if patrol_recoder.result==1:
                        mistake+=1
                deserve_list=patrol_device_items[facility.id]
                uncheck_device = 0
                print(666666)
                for patrol_device_items_set in set(deserve_list):
                    print(777)
                    if deserve_list.count(patrol_device_items_set)-partrol_table_actual.count(patrol_device_items_set)>0:
                        uncheck_device+=1
                        print(88)
                    if uncheck_device != 0:
                        print(99)
                        data.append({"facility_name": facility_name, "facility_location": facility_location,
                                    "facility_device_amount": len(device_list_day),
                                    "check_device":   len(device_list_day)-uncheck_device, "uncheck_device": uncheck_device,
                                    "mistake": mistake, "facility_user":",".join(list(set(username))), "date": patrol_date.strftime("%F"),
                                    "is_complete": 1, "detail": ''.join(more_massage)
                                        })
                    else:
                        data.append({"facility_name": facility_name, "facility_location": facility_location,
                                    "facility_device_amount": len(device_list_day),
                                    "check_device":   len(device_list_day)-uncheck_device, "uncheck_device": uncheck_device,
                                    "mistake": mistake, "facility_user":",".join(list(set(username))), "date": patrol_date.strftime("%F"),
                                    "is_complete": 0, "detail": ''.join(more_massage)
                                        })
        tmp['success'] = 1
        tmp['error_code'] = None
        tmp['error_msg'] = None
        tmp['data'] = data
        return jsonify(tmp)
"""


# @mod.route('/get_by_district/',methods=['POST'])
# def get_by_district_id():
#     try:
#         data=json.loads(request.data.decode("utf-8"))
#     except:
#         error_msg={
#                                         "success":0,
#                                         "error_code":-1,
#                                         "error_msg":"json format error",
#                                         "data":[]
#                                 }
#         return jsonify(error_msg)
#     district_id=data["id"]
#     facilities=District.get_facilities_by_district_id(district_id)
#     rlist=[]
#     for item in facilities:
#         devices=FacilityWithDevice.get_device_by_facility(item.id)
#         patrol_manages={}
#         for iitemA in devices:
#             # patrol_manages为一个字典,键为设备的id,值为这个设备的巡检项列表
#             patrol_manages[iitemA.id]=PatrolManage.get_patrol_manage_by_device_id(iitemA.id)
#         tlist=db_store.session.execute("SELECT patrol_time FROM patrol_recoder GROUP BY patrol_time;").fetchall()
#         patrol_dates=[]
#         for tt in tlist:
#             patrol_dates.append(tt[0])
#         # patrol_dates为以天为单位划分的巡检记录列表
#         for iitemB in patrol_dates:
#             one_day_patrol_facility_set=set()
#             one_day_patrol_facility_set.add(iitemB.device_id)


#  fanal拷贝过来的方法

@mod.route("/get_by_district/",methods=['POST'])
def get_result_recoder():

    #  得出记录统计结果

    data = []
    tmp = {}
    receive = request.data.decode("utf-8")
    dict = json.loads(receive)
    if 'id' not in dict:
        tmp['success'] = 0
        tmp['error_code'] = -9
        tmp['error_msg'] = "数据格式错误，请重新选择"
        tmp['data'] = data
        return jsonify(tmp)
    id = dict['id']  # 得到的是分区的id
    # 通过分区获取设施
    facilities = Facility.get_facilities_by_district_id(id)# 得到设施列表
    print('facilities', facilities) # [<Facility 12.00>, <Facility 18.00>, <Facility 19.00>]
    for facility in facilities:
        tlist=db_store.session.execute("SELECT patrol_time FROM patrol_recoder GROUP BY patrol_time;").fetchall()
        checked_days=[]
        for tt in tlist:
            print('tt', tt)  # tt (datetime.date(2017, 1, 1),)
            print('tt[0]', tt[0])  # tt[0] 2017-01-01
            checked_days.append(tt[0])
        devices= FacilityWithDevice.get_device_by_facility(facility.id)  # 12 >> 2
        print('devices', devices) # 第一次遍历出devices [<Device 设备1>] id=2
        # facility_with_devices = FacilityWithDevice.get_device_by_facility(facility.id)
        # print('facility_with_devices', facility_with_devices)
        all_time_list=[]
        for day in checked_days:
            one_day_dic={}    # {facility_id:[[device_id_1,device_id_2],facility_name,facility_location,should_check_num,uncheck_num,checked_num,check_user,unchecked_patrol_manage_num,if_complete,remark,date], ...}
            recoder_in_one_day=PatrolRecoder.query.filter_by(patrol_time=day).all() # 一天的查询记录6个 [<PatrolRecoder 26+2017-01-01>, <PatrolRecoder 27+2017-01-01>]
            print('recoder_in_one_day', recoder_in_one_day)
            facility_in_one_day=set()    # 当天巡检过的设施id的集合
            device_in_one_day=set()    # 当天巡检过的设备id的集合
            for recoder in recoder_in_one_day:
                f_id=FacilityWithDevice.query.filter_by(device_id=recoder.device_id).first().facility_id  # 通过d查f 2>,12
                print('f_id', f_id) # 2# 设施的id
                facility_in_one_day.add(f_id)   # 一天的设施id[2, ?12有吗]
                device_in_one_day.add(recoder.device_id)  # 一天的设备id[2]
            for in_facility_id in facility_in_one_day:   # 2,12
                for device_ONeDay in device_in_one_day:  # device_ONeDay > t
                    print("device_ONeDay", device_ONeDay) # 2
                    for p_m in PatrolManage.query.filter_by(device_type_id=Device.get_device_by_id(device_ONeDay).device_type_id).all():
                        print("p_m", p_m) # 设备管理项 p_m > 6个
                        # print("in for value: %s" %str(PatrolRecoder.query.filter_by(device_id=t).filter_by(patrol_manage_id=l.id).filter_by(patrol_time=day).all()))
                        p_r = PatrolRecoder.query.filter_by(device_id=device_ONeDay).filter_by(patrol_manage_id=p_m.id).filter_by(patrol_time=day).all()
                        print('p_r', p_r)
                        if len(p_r)!=1:
                            print("同一设备同一天没有被巡检")
                            # return abort(500)
                one_day_dic[in_facility_id]=[]
                one_day_dic[in_facility_id].append([d for d in device_in_one_day if FacilityWithDevice.query.filter_by(device_id=d).first().facility_id==in_facility_id])    #0 should checked device id
                print('1111one_day_dic[in_facility_id]', one_day_dic[in_facility_id][0])
                facility =Facility.get_facility_by_id(in_facility_id)  # facility > f
                # one_day_dic : {'in_facility_id':[2,name,经纬度,总设备数量,uncheck_num,check]}
                one_day_dic[in_facility_id].append(facility.name)    #1 facility name
                one_day_dic[in_facility_id].append("( "+str(facility.lat)+","+str(facility.lng)+" )")    #2 facility location
                one_day_dic[in_facility_id].append(len(FacilityWithDevice.get_device_by_facility(facility.id)))    #3 should check num
                # print("222one_day_dic[in_facility_id][0]: %s" %str(one_day_dic[in_facility_id][0]))
                print(111)
                # one_day_dic[in_facility_id].append(len(one_day_dic[in_facility_id][3])-len(one_day_dic[in_facility_id[0]]))    #4 uncheck num 原来的错误:object of type 'int' has no len()
                one_day_dic[in_facility_id].append(one_day_dic[in_facility_id][3] - 1)   #4 没有检查的设备好像有问题我的方法
                print(222)
                # one_day_dic[in_facility_id].append(one_day_dic[in_facility_id][3]-one_day_dic[4])    # checked num原来的  应该不对
                one_day_dic[in_facility_id].append(one_day_dic[in_facility_id][3] - one_day_dic[in_facility_id][4]) #5 已经检查的设备

                user_list = []
                for d in one_day_dic[in_facility_id][0]:   # d > t 设备id
                    # p_id = User.query.filter_by(
                    #     id=PatrolRecoder.query.filter_by(device_id=d).filter_by(patrol_time=day).first().patrol_user_id)
                    user_list.append(User.query.filter_by(
                        id=PatrolRecoder.query.filter_by(device_id=d).filter_by(patrol_time=day).first()
                            .patrol_user_id).first().username)  #1 username
                    # print('p_id', p_id.username)
                    # P = PatrolRecoder.query.filter_by(device_id=d).filter_by(patrol_time=day).first()
                    # print('P', P.patrol_user_id) # 2
                    # u = User.query.filter_by(id=P.patrol_user_id).first()
                    # print('u', u.username) # u <User 'test_usex1raaax'>
                    print('user_list',user_list)
                one_day_dic[in_facility_id].append(",".join(list(set(user_list))))    # check user #6[user_list]
                should_check_patrol_manage_num=0
                print('@@@@@')
                ###这个好像前端不需要显示
                for d in FacilityWithDevice.get_device_by_facility(in_facility_id): # d > 设备
                    should_check_patrol_manage_num += len(PatrolManage.get_patrol_manage_by_device_id(d.id)) # 应该巡检管理记录
                one_day_dic[in_facility_id].append(should_check_patrol_manage_num)  #7 应该巡检管理记录
                has_checked_patrol_manage_num=0
                print('!!!!!!!!')
                for d in one_day_dic[in_facility_id][0]:
                    has_checked_patrol_manage_num+=len(PatrolRecoder.query.filter_by(device_id=d).filter_by(patrol_time=day).all()) # 根据时间查出记录
                one_day_dic[in_facility_id].append(should_check_patrol_manage_num-has_checked_patrol_manage_num)     #8 unchecked patrol manage num
                # one_day_dic[in_facility_id].append(int(should_check_patrol_manage_num>has_checked_patrol_manage_num))    #9 if complete 0-is complete 1-not complete
                one_day_dic[in_facility_id].append(int(should_check_patrol_manage_num > has_checked_patrol_manage_num))    #9 if complete 0-is complete 1-not complete
                # print(int(should_check_patrol_manage_num > has_checked_patrol_manage_num))
                # print('到了吗????')
                t_list=[]
                for d in one_day_dic[in_facility_id][0]:
                    recoder_of_the_device_in_the_day_list=PatrolRecoder.query.filter_by(device_id=d).filter_by(result=1).filter_by(patrol_time=day).all() # 有故障的设备数
                    for m in recoder_of_the_device_in_the_day_list:
                        if m.remark is None:
                            t_list.append("没有故障说明")
                        else:
                            t_list.append(m.remark)
                t_str=",".join(t_list)
                one_day_dic[in_facility_id].append(len(recoder_of_the_device_in_the_day_list)) #10 有故障的设备数
                one_day_dic[in_facility_id].append(t_str)    #11 patro l remark
                one_day_dic[in_facility_id].append(day.strftime("%Y-%m-%d"))    #12 patrol date
            print('$$$$$$$$')
            # one_day_dic: {'in_facility_id': [2, name, 经纬度, 总设备数量, uncheck_num, check(5)]}
            # for t in one_day_dic:  # {'in_facility_id': []}
            tdic={}
            tdic["facility_name"]=one_day_dic[in_facility_id][1]
            tdic["facility_location"]=one_day_dic[in_facility_id][2]
            tdic["facility_device_amount"]=one_day_dic[in_facility_id][3]
            tdic["uncheck_device"]=one_day_dic[in_facility_id][4]
            tdic["check_device"]=one_day_dic[in_facility_id][5]
            tdic["facility_user"]=one_day_dic[in_facility_id][6]
            tdic["should_check_patrol_manage_num"]=one_day_dic[in_facility_id][7]
            tdic["unchecked_patrol_manage_num"]=one_day_dic[in_facility_id][8]
            tdic["is_complete"] = one_day_dic[in_facility_id][9]  # 0表示完成, 1 未完成
            tdic["breakdown_num"] = one_day_dic[in_facility_id][10] # 有故障设备数
            tdic["patrol_remark"]=one_day_dic[in_facility_id][11]
            tdic["patrol_date"]=one_day_dic[in_facility_id][12]
            all_time_list.append(tdic)
    rdic={}
    rdic["success"]=1
    rdic["error_code"]=None
    rdic["error_msg"]=None
    rdic["data"]=all_time_list
    return jsonify(rdic)


@mod.route("/search/",methods=["POST"])
def search_patrol_recorder():
    """
    查询设备信息
    """
    # request: json
    # {
    #      "query_word": "lishimin"
    # }
    if request.method == 'POST':
        dict = json.loads(request.data.decode('utf-8'))
        tmp ={}
        if 'query_word' in dict:
            query_word = dict['query_word']
        else:
            tmp['success'] = 0
            tmp['error_code'] = -9
            tmp['error_msg'] = "数据格式错误"
            tmp['data'] = []
            return result(success=0,error_code=-9,error_msg='数据格式错误')
        patrol_recorders = PatrolRecoder.search(query_word)
        return result(success=1,data=patrol_recorders)


